<?php
declare(strict_types=1);

namespace App\Listener;

use App\AsyncTask\OcpcJobMessage;
use App\Helpers\OcpcUtil;
use App\Model\Service\OcpcService;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\OnPipeMessage;
use Hyperf\Process\Event\PipeMessage as UserProcessPipeMessage;
use Hyperf\ConfigCenter\Listener\OnPipeMessageListener;
use Hyperf\Event\Annotation\Listener;

/**
 * 回传消息事件监听
 * @Listener
 */
class AsyncTaskListener implements ListenerInterface
{
    /**
     * @return string[] returns the events that you want to listen
     */
    public function listen(): array
    {
        return [
            OnPipeMessage::class,
            UserProcessPipeMessage::class,
        ];
    }

    public function process(object $event)
    {
        if ($event instanceof OnPipeMessage || $event instanceof UserProcessPipeMessage) {
            if ($event->data instanceof OcpcJobMessage) {
                $service = new OcpcService($event->data);
                $service->handle();
                return;
            }
        }
    }
}
